package com.t309.tzy.controller;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

import java.io.IOException;

@Controller
//@RequestMapping( "/user" )
public class TestControllerDemo {
    private Logger log = Logger.getLogger("UserControllerDemo");
    /**
     * 父容器(Spring) 可以调用 子容器 (Spring MVC), IoC(控制反转)创建IUserService对象
     */

    //@Resource
    //private ITsysUserService userService;

    /*@GetMapping("/view/{id}")
    //@GetMapping("/{id}/view")
    //@GetMapping("/{id}")
    public String test( @PathVariable Integer id,
@PathVariable String name,
 Model model ){
        log.debug("根据id查询用户详情"+id);
        final User user = userService.getUserInfoByIdOrUsername(id, null);
        log.debug("user.getNickName()="+user.getNickName());
        model.addAttribute(user);
        return "WEB-INF/jsp/view";
    }*/



    /**
     * 跳转到学生注册页面
     *//*

    @RequestMapping("studentSignUp")
    public ModelAndView toStudentSignUpPage() {
//        modelAttribute这个表单的所有内容显示绑定的是请求域中Student的值
        System.err.println("==正在前往登录页面==");
        ModelAndView mv=new ModelAndView("/client/student");
        mv.addObject(new User());
        log.debug("debug: "+mv.getModelMap());
        return mv;
    }

    @GetMapping("/doLoginForm")
    public String doLoginForm(User user,HttpSession session){
        if(userService.login(user)>0){
            log.debug("user.account"+user.getAccount());


            return "client/view";
        }
        //return "redirect:/manager/user/usersList.jsp";//重定向
        return "/client/student";//转到当前视图
    }
//————————————————
//    版权声明：本文为CSDN博主「二楼后座。」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
//    原文链接：https://blog.csdn.net/D1124615130/article/details/104927880

    */
/*@GetMapping("/toLoginForm")
    public String toLoginForm(@ModelAttribute("user") User user ){
        System.err.println("[ 正在前往注册页面 ]");
        //转到指定视图名( WEB-INF/jsp/loginForm ).jsp
        return "client/loginForm";
    }*//*





    @GetMapping("/view/login") //    @PathVariable User user
    public String registerTest(@Valid User user , BindingResult bindingResult,
                               HttpSession session){
        if(bindingResult.hasErrors()){
            log.debug("失败");
            return "WEB-INF/jsp/error";
        }
        return "WEB-INF/jsp/view";
    }

    */
/*@RequestMapping(value="toLoginForm")
    public String toLoginFrom(Model model){
        User user=new User();
        user.setNickName("GFFDG");
        user.setAccount("24");
        //注意，此时名称是command
        model.addAttribute("command",user);
        return "loginForm";
    }*//*








    @GetMapping(value="/login")
    public String login(){
        System.err.println("TSysUserController => /login");
        log.debug("TSysUserController : login()==================");
        //跳转到指定视图 //跳转到 client/login.jsp
        //return "WEB-INF/jsp/login";
        return "client/login";
    }

    */
/**
     * 登录控制
     * @param model     出参
     * @param account   入参
     * @param password  入参
     * @param request
     * @param response
     * @param session
     * @return
     * @throws IOException
     *//*

    @GetMapping("/doLogin")
    public String doLogin(Model model,@RequestParam String account, @RequestParam String password,
                          HttpServletRequest request, HttpServletResponse response,
                          HttpSession session) throws IOException {
        PrintWriter out = response.getWriter();
        log.debug("doLogin====================================");
        //调用service方法，进行用户匹配
        final int login = userService.login(account, password);
        session = request.getSession();
        session.setAttribute( SessionConstant.USERNAME_IN_SESSION, account );
        model.addAttribute("username", account);
        model.addAttribute("password", password);
        log.info("model.getAttribute(\"username\")="+model.getAttribute("username") );
        //User user = new User(account, password);
        if(login>0){    //登录成功
            log.info("doLogin() : 登录成功");
            out.println("==true==");
            //放入session
            session.setAttribute(SessionConstant.USERNAME_IN_SESSION, account);
            log.info("SessionConstant.USERNAME_IN_SESSION="+session.getAttribute(SessionConstant.USERNAME_IN_SESSION ));
            //页面跳转（xxx.jsp）
            return "WEB-INF/jsp/success";//无法跳转 : return "/index";

        }else{
            out.println("doLogin() : 登录失败 false !");
            //页面跳转（login.jsp）带出提示信息--转发
            request.setAttribute("error", "用户名或密码不正确");
            //request.getSession().setAttribute("error", "用户名或密码不正确");
            //跳转到 失败视图名
            return "WEB-INF/jsp/error";//无法跳转 : return "client/login";
        }
    }

    @GetMapping("/register")
    protected String register() {
        System.err.println("TSysUserController => /register");
        //跳转到指定视图 //跳转到 login.jsp
        //return "WEB-INF/jsp/register";
        return "client/register";
    }

    */
/**
     * 注册控制
     * @param model
     * @param account
     * @param password
     * @param nickName
     * @param request
     * @param response
     * @param session
     * @return
     * @throws IOException
     *//*

    @GetMapping(value="/doRegister")
    public String doRegister(Model model,@RequestParam String account, @RequestParam String password,
        @RequestParam String nickName,@RequestParam Integer gender,@RequestParam String idCardNo,
                             @RequestParam String email,@RequestParam String phone,
     HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException
    {
        PrintWriter out = response.getWriter();
        log.debug("doRegister====================================");
        // 性别改成变量,不写死
        User user = new User(null, account, nickName, password, gender,
                idCardNo, email, phone, null);
        System.err.println("user.getAccount()="+user.getAccount()+"user.getGender()="+user.getGender());
        //User user = new User( account, password, nickName);
        //调用service方法，进行用户匹配
        final int register = userService.insert(user);
        log.debug("受影响的行数 : "+register);
        if(register>0){ //注册成功
            log.info("doRegister() : 注册成功");
            out.println("==true==");
            model.addAttribute("user",user);
            //放入session
            //session.setAttribute(Constants.USER_SESSION, user);
            //页面跳转 （frame.jsp）
            return "WEB-INF/jsp/success";
        }else{
            log.info("doRegister() : 注册失败 !");
            out.println("doRegister() : 注册失败 false !");
            //页面跳转（login.jsp）带出提示信息--转发
            request.setAttribute("error", "注册失败 !!");
            return "WEB-INF/jsp/register";
        }
    }

    @GetMapping(value="/index")
    public String index(HttpSession session){
        log.debug("TSysUserController : index()==================");
        */
/*if(session.getAttribute(Constants.USER_SESSION) == null){
            return "redirect:/user/login.html";
        }*//*

        //跳转到/WEB-INF/jsp/welcome.jsp ( 前缀(项目名) + 视图名 + 后缀(.jsp) ) in springmvc-servlet.xml
        return "welcome";
    }

    @GetMapping("/list/{id}")
    public String getUser(@PathVariable int id, Model model){
        final List<User> usersList = userService.getAllUsersByPage(0, 10);
        model.addAttribute("usersList",usersList);
        return "/manager/user/usersList";
    }

    @GetMapping("/usersList")
    public String usersList(){
        System.err.println("TSysUserController => /usersList");
        log.debug("TSysUserController : usersList()==================");
        //跳转到指定视图 //跳转到 login.jsp
        return "WEB-INF/jsp/usersList";
    }

    @GetMapping(value="/doUsersList")
    public String doUsersList(Model model,@RequestParam String account, @RequestParam String password,
                          HttpServletRequest request, HttpServletResponse response,
                          HttpSession session) throws IOException {
        PrintWriter out = response.getWriter();
        log.debug("doUsersList====================================");
        //调用service方法，进行用户匹配
        final List<User> list = userService.getAllUsersByPage(0, 6);

        return "WEB-INF/jsp/usersList";
    }



    */
/*
//  id:入参, model:出参
    public String index(@RequestParam User user, Model model){ }*//*
     */
}
